<template>
	<view class="content">

		<view class="user_from qcfd" style="background-color: #FFFFFF;">
			<text class="fl impot_font">取货地址:</text><input placeholder-style="font-size:28rpx;color:#ACACAC;"
				v-model="deliver_adress" class="uni-input fl" placeholder="请输入取货地址" />
		</view>
		<view class="user_from qcfd" style="background-color: #FFFFFF;">
			<text class="fl impot_font">联系人 :</text><input    placeholder-style="font-size:28rpx;color:#ACACAC;" 
				v-model="deliver_name" class="uni-input fl" placeholder="请输入联系人(选填)" />
		</view>
		<view class="user_from qcfd" style="background-color: #FFFFFF;">
			<text class="fl impot_font">取货电话 :</text><input  type="number" placeholder-style="font-size:28rpx;color:#ACACAC;" @blur="yzsjg"
				v-model="deliver_phone" class="uni-input fl" placeholder="请输入取货电话(选填)" />
		</view>
		<view class="deliver_info">
			<button @click="submitDeliver" type="default"  :disabled="jyan" :loading="jyan">确定</button>
		</view>


	</view>
</template>

<script>
	import requestUrl from '@/index.js'
	export default {
		data() {
			return {
				deliver_name: '',
				deliver_phone: '',
				deliver_adress:'',
				deliver_:{},
				tmdid:0,
				ydlist:[],
				jyan:false
			}
		},
		onShow() {
		  if(uni.getStorageSync("token")){
			  this.getDeliver()
		}
		//  else if(uni.getStorageSync("txfhr")){
			
		// }
		},
		
		onLoad(option) {
			
			if(option.data){
				console.log('option',option)
				this.tmdid= JSON.parse(option.data).id
				this.deliver_adress= JSON.parse(option.data).address
				if(JSON.parse(option.data).sender!==null&&JSON.parse(option.data).sender!=='- -'){
					this.deliver_name= JSON.parse(option.data).sender
				}else{
					this.deliver_name=''
				}
				 if(JSON.parse(option.data).senderPhone!==null&&JSON.parse(option.data).senderPhone!=='- -'){
					this.deliver_phone= JSON.parse(option.data).senderPhone
				}else{
					this.deliver_phone=''
				}
			}
			
		},
		
		methods: {
			async hdqhr(){
				let ress = await this.$request.get(this.$requestUrl.GET_ADRESS_URL, {
					data: {
						type: 3,
						isTms: 2
					}
				});
				
				if (ress.length > 0) {
					this.ydlist = ress
				}
				uni.setStorageSync('qurecever',ress[ress.length-1])
			},
			async yzsjg(evt){
				let oldphone = evt.target.value
				console.log(oldphone)
				let phonereg=11&&/^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
				if(! phonereg.test(oldphone)&&oldphone){
					return this.$api.msg('请填写正确的手机号')
				}
				
				// if (oldphone !== '' && uni.getStorageSync('token')) {
				// 	await this.hdqhr()
				// 	for (let i = 0; i < this.ydlist.length; i++) {
				// 		if (this.ydlist[i]['senderPhone'] == oldphone) {
				// 			return this.$api.msg('客户信息已存在，请点击取货地址簿选择')
				// 		}
				// 	}
				// }
				
				},
			async submitDeliver() {
				 if(!uni.getStorageSync("token")){
					this.deliver_.deliver_adress=this.deliver_adress
					this.deliver_.deliver_name=this.deliver_name
					this.deliver_.deliver_phone=this.deliver_phone
					uni.setStorageSync('txqhr',this.deliver_)
				return	uni.showModal({
					    title: '提示',
					    content: '您未登录,是否跳转到登录页',
					    success:await function (res) {
					        if (res.confirm) {
					            uni.switchTab({
					            	url:'../mine/mine'
					            })
					        } else if (res.cancel) {
					           
					        }
					    }
					});
				}
				let phonereg=11&&/^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
				if(this.deliver_adress==''){
					return this.$api.msg('请填写取货地址')
				}else if(this.deliver_phone!=='' &&!phonereg.test(this.deliver_phone)){
					console.log('请填写正确的手机号',this.deliver_phone)
					return this.$api.msg('请填写正确的手机号')
				}
				
				this.jyan=true
				if(this.tmdid==0){
					try{
						var resz = await this.$request.post(this.$requestUrl.NEW_ADDRESS, {
							data: {
								sender: this.deliver_name,
								senderPhone:this.deliver_phone,
								address:this.deliver_adress,
								type: 3,
								isTms:2
							}
						});
					}catch(e){
						this.$api.msg('网络错误，请重试')
						return this.jyan=false
					}
					if (resz){
						await this.hdqhr()
						
						this.jyan=false
						uni.navigateBack({
							delta: 1
						})
					}else{
						this.$api.msg(resz|| '系统繁忙，请稍后重试')
						return this.jyan=false 
					}
				}else{
					try{
						var resz = await this.$request.put(this.$requestUrl.EDIT_ADRESS_URL, {
							data: {
								id: this.tmdid,
								sender: this.deliver_name,
								senderPhone:this.deliver_phone,
								address:this.deliver_adress,
								type: 3,
								isTms:2
						
							}
						}); //调取API接口
					}catch(e){
						this.$api.msg('网络错误，请重试')
						return  this.jyan=false 
					}
					
					if (resz){
						uni.navigateBack({
							delta: 1
						})
					}else{
						this.$api.msg(resz|| '系统繁忙，请稍后重试')
						 this.jyan=false 
					}
					
					
				}
				
				
				
			},
			async	getDeliver(){
				if(uni.getStorageSync('txqhr')){
					this.deliver_name=uni.getStorageSync('txqhr').deliver_name
					this.deliver_adress=uni.getStorageSync('txqhr').deliver_adress
					this.deliver_phone=uni.getStorageSync('txqhr').deliver_phone
					uni.removeStorage({
						key: 'txqhr'
					})
					
				}
				
				
			},
		
		}
	}
</script>

<style>
	.content {
		position: relative;
		height: 100vh;
	}

	.user_from {
		padding: 26rpx;
		border-radius: 8rpx;
		margin: 24rpx 22rpx;
	}


	.impot_font {
		font-size: 28rpx;
		font-weight: 500;
	}

	.deliver_info {
		width: 100%;
		padding: 10rpx 0;
		position: fixed;
		bottom: 0rpx;
		background-color: #FFFFFF;
		padding-bottom: 30rpx;
	}

	.deliver_info button {
		width: 702rpx;
		height: 80rpx;
		line-height: 80rpx;
		margin: 0 auto;
		background-color: #FF690D;
		border-radius: 40rpx !important;
		color: #FFFFFF;
	}
	 input{
		 margin-left: 30rpx;
		 width: 450rpx;
		 overflow: hidden;
		 	white-space: nowrap;
		 		text-overflow: ellipsis;
	 }
</style>

 
 
 